/**
 * Created by ht on 2016/1/23.
 * 日期选择
 */
var dateTimePicker = angular.module("date-time-picker",[]);

/**
 * 日期选择
 * 只展示日期的选择：date-format="yy-mm-dd" (可选)
 * 展示时间的选择： date-format="yy-mm-dd"（可选） time-format="HH:mm:ss"
 */
dateTimePicker.directive('dateTimePicker', function() {
    return {
        restrict: "A",
        link: function(scope, element, attrs) {
            var dateFormat = attrs['dateFormat']?attrs['dateFormat']:"yy-mm-dd";
            var timeFormat = attrs['timeFormat'];
            var options = {
                dateFormat: dateFormat,
                changeYear:true
            };
            if(timeFormat) {
                options.timeFormat = timeFormat;
                element.datetimepicker(options);
            } else{
                element.datepicker(options);
            }

            var model = attrs['ngModel'];

            element.attr('readonly', true);
            element.css('cursor', 'pointer');
            element.change(function() {
                scope[model] = this.value;
                scope.$apply();
            });
            addTip();
            function addTip() {
                var tip = "<div class='glyphicon glyphicon-calendar' style='position: absolute;top:auto;margin-top: 6px;cursor: pointer;margin-left: -18px;'></div>";
                element.parent().append(tip);
                element.parent().find('.glyphicon-calendar').click(function() {
                    element.trigger("focus");
                });
            }

        }
    }
});